﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using WeChat.PlatForm.Entity;

namespace WeChat.PlatForm.DataAccess
{
    public class MyDBContext : DbContext
    {
        public DbSet<PersonEntity> Persons { get; set; }

        public DbSet<SchoolEntity> Schools { get; set; }

        public DbSet<RoleEntity> Roles { get; set; }

        public DbSet<UserEntity> Users { get; set; }

        public DbSet<UserRoleEntity> UserRoles { get; set; }



        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.UseMySql("Server=101.200.32.14;database=BCJ;uid=root;pwd=myk123");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            var etPerson = modelBuilder.Entity<PersonEntity>();
            etPerson.ToTable("Person").HasOne(a => a.School).WithMany().HasForeignKey(a => a.SchoolId);

            var etsChool = modelBuilder.Entity<SchoolEntity>();
            etsChool.ToTable("School");

            var etsRole = modelBuilder.Entity<RoleEntity>();
            etsRole.ToTable("T_ROLE");

            var etsUser = modelBuilder.Entity<UserEntity>();
            etsUser.ToTable("T_USER");

            var etsUserRole = modelBuilder.Entity<UserRoleEntity>();
            etsUserRole.ToTable("T_USER_ROLE");

            etsUserRole.HasOne(a => a.User).WithMany().HasForeignKey(a => a.USER_ID);
            etsUserRole.HasOne(a => a.Role).WithMany().HasForeignKey(a => a.ROLE_ID);
        }
    }
}
